jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var cdnurl = 'http://hua-static.fengqiaoju.com';

$(document).ready(function(){
	var uid = $('body').attr('data-uid');
	$('.content img').removeAttr('height');
	$('.content img').removeAttr('width');
	//注册
	$('.home-register-btn').click(function(){
		var url = '/?c=Ajax&a=user&do=register';
		var obj = $('.home-register-form');
		var username = obj.find('[name=username]').val();
		var email = obj.find('[name=email]').val();
		var password = obj.find('[name=password]').val();
		var password1 = obj.find('[name=password1]').val();
		obj.find('.tip').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		if(!username){
			obj.find('[name=username]').addClass('error');
			obj.find('.tip').html('用户名不能空');
			return ;
		}else{
			obj.find('[name=username]').removeClass('error')
		}
		if(!email){
			obj.find('[name=email]').addClass('error');
			obj.find('.tip').html('邮箱不能空');
			return ;
		}else{
			obj.find('[name=email]').removeClass('error')
		}
		if(!password){
			obj.find('[name=password]').addClass('error');
			obj.find('.tip').html('密码不能空');
			return ;
		}else{
			obj.find('[name=password]').removeClass('error')
		}
		if(password!=password1){
			obj.find('[name=password1]').addClass('error');
			obj.find('.tip').html('密码不一致');
			return ;
		}else{
			obj.find('[name=password1]').removeClass('error')
		}
		data = {'username':username,'email':email,'password':password};
		$.post(url,data,function(rs){
			if(rs.status == 1){
				location.reload();
			}else{
				obj.find('.tip').html(rs.msg);
			}
		});
	});
	//注册框 用户名检查
	$('.home-register-form').find('[name=username]').blur(function(){
		var obj = $('.home-register-form');
		var username = obj.find('[name=username]').val();
		if(!username){
			obj.find('[name=username]').addClass('error');
			obj.find('.tip').html('用户名不能空');
			return ;
		}else{
			obj.find('.tip').html('');
			obj.find('[name=username]').removeClass('error')
		}																	
	});		
	//注册框 邮箱检查
	$('.home-register-form').find('[name=email]').blur(function(){
		var obj = $('.home-register-form');
		var username = obj.find('[name=email]').val();
		if(!username){
			obj.find('[name=email]').addClass('error');
			obj.find('.tip').html('邮箱不能空');
			return ;
		}else{
			obj.find('.tip').html('');
			obj.find('[name=email]').removeClass('error')
		}																	
	});	
	//注册框 密码检查
	$('.home-register-form').find('[name=password]').blur(function(){
		var obj = $('.home-register-form');
		var password = obj.find('[name=password]').val();
		if(!password){
			obj.find('[name=password]').addClass('error');
			obj.find('.tip').html('密码不能空');
			return ;
		}else{
			obj.find('.tip').html('');
			obj.find('[name=password]').removeClass('error')
		}																
	});
	
	
	//登录
	$('.home-login-btn').click(function(){
		var url = '/?c=Ajax&a=user&do=login';
		var obj = $('.home-login-form');
		var username = obj.find('[name=username]').val();
		var password = obj.find('[name=password]').val();
		obj.find('.tip').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		if(!username){
			obj.find('[name=username]').addClass('error');
			obj.find('.tip').html('用户名不能空');
			return ;
		}else{
			obj.find('[name=username]').removeClass('error')
		}
		if(!password){
			obj.find('[name=password]').addClass('error');
			obj.find('.tip').html('密码不能空');
			return ;
		}else{
			obj.find('[name=password]').removeClass('error')
		}
		data = {'username':username,'password':password};
		$.post(url,data,function(rs){
			if(rs.status == 1){
				location.reload();
			}else{
				obj.find('.tip').html(rs.msg);
			}
		});
	});
	//登录框 用户名检查
	$('.home-login-form').find('[name=username]').blur(function(){
		var obj = $('.home-login-form');
		var username = obj.find('[name=username]').val();
		if(!username){
			obj.find('[name=username]').addClass('error');
			obj.find('.tip').html('用户名不能空');
			return ;
		}else{
			obj.find('.tip').html('');
			obj.find('[name=username]').removeClass('error')
		}																	
	});	
	//登录框 密码检查
	$('.home-login-form').find('[name=password]').blur(function(){
		var obj = $('.home-login-form');
		var password = obj.find('[name=password]').val();
		if(!password){
			obj.find('[name=password]').addClass('error');
			obj.find('.tip').html('密码不能空');
			return ;
		}else{
			obj.find('.tip').html('');
			obj.find('[name=password]').removeClass('error')
		}																
	});
	//登录框 回车
	$('.home-login-form').find('input').keydown(function(e){
		if(e.keyCode==13){
			$('.home-login-btn').click();
		}
	});
	
	//留言反馈
	$('.home-guestbook-btn').click(function(){
		var url = '/?c=Ajax&a=guestbook';
		var obj = $('.home-guestbook-form');
		var contact = obj.find('[name=contact]').val();
		var context = obj.find('[name=feedContent]').val();
		var data = {'contact':contact,'context':context};
		obj.find('.title>.tip').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		$.post(url,data,function(rs){
			if(rs.status == 1){
				obj.find('.title>.tip').html('您的反馈提交成功！');
				obj.find('[name=contact]').val('');
				obj.find('[name=feedContent]').val('');
				obj.find('.btm').html('');
				obj.find('.input-box').html('&nbsp;&nbsp;<a href="http://www.wenjuan.com/s/B3auy2">非常感谢您的建议反馈！点击进一步参与我们的用户调查。</a>');
			}else{
				obj.find('.title>.tip').html(rs.msg);
			}
		});												
	});	
	
	//发表日志
	$('.home-blog-btn').click(function(){
		var url = '/?c=Ajax&a=blog&do=add';
		var obj = $('.home-blog-form');
		var title = obj.find('[name=contact]').val();
		var context = obj.find('[name=writeContent]').val();
		var data = {'title':title,'context':context};
		obj.find('.title>.tip').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		$.post(url,data,function(rs){
			if(rs.status == 1){
				obj.find('.title>.tip').html('');
				obj.find('[name=contact]').val('');
				obj.find('[name=writeContent]').val('');
				location.href= '/?a=blogshow&id='+rs.msg;
			}else{
				obj.find('.title>.tip').html(rs.msg);
			}
		});												
	});
	
	//评论
	$('.home-comment-btn').click(function(){
		var url = '/?c=Ajax&a=comment&do=add';
		var obj = $('.home-comment-form');
		var context = obj.find('[name=context]').val();
		var obj_id = obj.find('[name=obj_id]').val();
		var obj_type = obj.find('[name=obj_type]').val();
		var data = {'obj_type':obj_type,'obj_id':obj_id,'context':context};
		obj.find('span').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		$.post(url,data,function(rs){
			if(rs.status == 1){
				getComment(obj_id,obj_type,'clear');
				obj.find('span').html('');
				obj.find('[name=context]').val('');
				obj.find('span').html(rs.msg);
			}else{
				obj.find('span').html(rs.msg);
			}
		});												
	});	
	
	//搜索
	$('.home-search-btn').click(function(){
		var keyword = $('.home-search-value').val();
		location.href = '/name/'+keyword+'/';
	});
	$('.home-search-value').keydown(function(e){
		if(e.keyCode==13){
			var keyword = $(this).val();
			location.href = '/name/'+keyword+'/';
		}
	});
	
	//修改密码
	$('.home-repassword-btn').click(function(){
		var url = '/?c=Ajax&a=user&do=repassword';
		var obj = $('.home-repassword-form');
		var oldpassword = obj.find('[name=oldpassword]').val();
		var newpassword = obj.find('[name=newpassword]').val();
		var renewpassword = obj.find('[name=renewpassword]').val();
		if(newpassword != renewpassword){
			alert('2次新密码不一致');
			return false;
		}
		$.post(url,{'oldpassword':oldpassword,'newpassword':newpassword,'renewpassword':renewpassword},function(rs){
			if(rs.status == 1){
				obj.find('[name=oldpassword]').val('');
				obj.find('[name=newpassword]').val('');
				obj.find('[name=renewpassword]').val('');				
			}else{
				
			}
			alert(rs.msg);
		});
	});	
	
	//找回密码
	$('.home-findpassword-btn').click(function(){
		var url = '/?c=Ajax&a=user&do=findpassword';
		var obj = $('.home-findpassword-form');
		var email = obj.find('[name=email]').val();
		if(!email){
			obj.find('.title>.tip').html('请输入用户名或者注册邮箱');
			obj.find('[name=email]').addClass('error');
			return false;
		}else{
			obj.find('.title>.tip').html('');
			obj.find('[name=email]').removeClass('error');			
		}
		obj.find('span').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		$.post(url,{'email':email},function(rs){
			if(rs.status == 1){
				obj.find('[name=email]').val('');			
			}
			obj.find('.title>.tip').html(rs.msg);	
		});
	});	
	
	//重设新密码
	$('.home-refindpassword-btn').click(function(){
		var url = '/?c=Ajax&a=user&do=refindpassword';
		var obj = $('.home-refindpassword-form');
		var password = obj.find('[name=password]').val();
		var repassword = obj.find('[name=repassword]').val();
		var token = obj.find('[name=token]').val();
		var uid = obj.find('[name=uid]').val();
		if(!password){
			obj.find('.msg').html('密码不能空');
			obj.find('[name=password]').addClass('error');
			return false;
		}else{
			obj.find('.msg').html('');
			obj.find('[name=password]').removeClass('error');			
		}	
		
		if(password != repassword){
			obj.find('.msg').html('2次密码不相同');
			obj.find('[name=repassword]').addClass('error');
			return false;
		}else{
			obj.find('.msg').html('');
			obj.find('[name=repassword]').removeClass('error');			
		}
		obj.find('.msg').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		$.post(url,{'password':password,'token':token,'uid':uid},function(rs){
			if(rs.status == 1){
				obj.find('[name=password]').val('');	
				obj.find('[name=repassword]').val('');
			}
			obj.find('.msg').html(rs.msg);
			setTimeout("location.href = '/'",1000);
		});
	});	
	
	//重新激活按钮
	$('.home-reactivation-btn').click(function(){
		var url = '/?c=Ajax&a=user&do=sendActivation';
		$(this).html('<img src="'+cdnurl+'/home/images/loading.gif" style="width:15px; height:15px;" />');
		$.get(url,function(rs){
			$('.home-reactivation-btn').parent('span').html('<span style="color:#ccc;">['+rs.msg+']</span>');			   
		});
	});
	
	//获取用户评论列表 - 用户菜单
	$('.home-user-comment-btn').click(function(){
		$('.home-user-comment-nextbtn').attr('data-page',1);
		$('.home-user-comment-list div').html('');
		getUserComment();
	});
	//用户评论下一页
	$('.home-user-comment-nextbtn').click(function(){
		getUserComment();   
	});	
	
	//获取用户日志 - 用户菜单
	$('.home-user-blog-btn').click(function(){
		$('.home-user-blog-nextbtn').attr('data-page',1);
		$('.home-user-blog-list div').html('');
		getUserBlog();
	});
	//用户日志下一页
	$('.home-user-blog-nextbtn').click(function(){
		getUserBlog();   
	});
	
	//点击个人中心
	$('.usercenter').click(function(){
		$('.home-user-blog-nextbtn').attr('data-page',1);
		$('.home-user-blog-list div').html('');
		getUserBlog();		
	});
});

//获取评论
function getComment(obj_id,obj_type,clear){
	var obj = $('.home-comment-list');
	var page = obj.find('.home-comment-nextbtn').attr('data-page');
	var url = '/?c=Ajax&a=comment&do=list&obj_id='+obj_id+'&obj_type='+obj_type+'&page='+page;
	var html = '';
	$.get(url,function(rs){
		$.each(rs.list,function(i,val){
			html += '<li><div class="left"><img src="'+val.avatar+'" width="50" height="50" /></div><div class="right"><div class="info"><div class="name">'+val.username+'</div><div class="time">'+val.createtime+'</div></div><div class="content">'+val.text200+'</div></div></li>';
		});
		if(clear == 'clear'){
			obj.find('ul').html(html);
		}else{
			obj.find('ul').append(html);
		}
		if(rs.next_page){
			obj.find('.home-comment-nextbtn').attr('data-page',rs.next_page);
		}else{
			obj.find('.home-comment-nextbtn').hide();
		}
	});
}

//获取用户评论
function getUserComment(){
	var page = $('.home-user-comment-nextbtn').attr('data-page');
	var url = '/?c=Ajax&a=comment&do=getUserComment&page='+page;
	$('.home-user-comment-list').find('.loading').html('<img src="'+cdnurl+'/home/images/loading.gif" style="width:15px; height:15px;" />');
	$('.home-user-comment-nextbtn').hide();
	$.get(url,function(rs){
		html = '';
		if(rs.list){
			$.each(rs.list,function(i,val){
				html += '<li><a href="'+val.obj_info.url+'" target="_blank">'+(val.obj_info.title?val.obj_info.title:val.obj_info.name).substr(0,35)+'</a><br><em>&nbsp;&nbsp;&nbsp;&nbsp;我说：'+val.text200.substr(0,35)+'..</em></li>';
			});
			if(rs.next_page){
				$('.home-user-comment-nextbtn').attr('data-page',rs.next_page).show();
			}else{
				$('.home-user-comment-nextbtn').hide();
			}
		}else{
			html = '暂无信息';
		}		
		$('.home-user-comment-list div').append(html);
		$('.home-user-comment-list').find('.loading').html('');
	});	
}

//获取用户日志
function getUserBlog(){
	var page = $('.home-user-blog-nextbtn').attr('data-page');
	var url = '/?c=Ajax&a=blog&do=getUserBlog&page='+page;
	$('.home-user-blog-list').find('.loading').html('<img src="'+cdnurl+'/home/images/loading.gif" style="width:15px; height:15px;" />');
	$('.home-user-blog-nextbtn').hide();
	$.get(url,function(rs){
		html = '';
		if(rs.list){
			$.each(rs.list,function(i,val){
				html += '<li><a href="'+val.url+'" target="_blank">'+val.title.substr(0,35)+'</a></li>';
			});
			if(rs.next_page){
				$('.home-user-blog-nextbtn').attr('data-page',rs.next_page).show();
			}else{
				$('.home-user-blog-nextbtn').hide();
			}
		}else{
			html = '暂无信息';
		}
		$('.home-user-blog-list div').append(html);
		$('.home-user-blog-list').find('.loading').html('');
	});	
}


//新增小站访问数
function addView() {
	
	var arr = new Array();
	var type = $('#data-bar').attr('data-type');
	var id = $('#data-bar').attr('data-id');
	if(!type || !id){
		return false;	
	}
	var cookeName = 'hua@'+type+'_addview';
	var str = $.cookie(cookeName) ? $.cookie(cookeName) : 0;
	if (str) {
		arr = str.split(',');
	}
	if ($.inArray(id, arr) > 0) {
		return true;
	} else {
		var val = str + ',' + id;
		$.cookie(cookeName, val);
		$.get('/?c=Ajax&a='+type+'&do=addview&id=' + id);
		return false;
	}
}

////start
$(window).bind("scroll", function(){ 
	var s = $(window).scrollTop();
	var min_height = 200;
	if( s >= min_height){
	$("#goTop .top").fadeIn(1000);
	}else{
	$("#goTop .top").fadeOut(1000);
	}
});


//搜索自动补全
$(function(){
	var url = '/?a=keyword&c=Ajax';
    $("#mysearch").bigAutocomplete({url:url});
})


function btover(){
	$(".search-b").css({"background-image":"url("+cdnurl+"/home/images/icon.gif)","background-position:":"-124px 0px"});
}
function btout(){
	$(".search-b").css({"background-image":"url("+cdnurl+"/home/images/icon.gif)","background-position:":"-155px 0px"});
}

$(document).ready(function(){
	var uid = $('body').attr('data-uid');
	
	//回顶
	$("#goTop .top").click(function(){
		$('html,body').animate({scrollTop:0},700);
	});	
	
	//弹出层 
	var bodyH = document.body.offsetHeight; 
	var bodyyes = document.documentElement.clientHeight; 
	
	
	//留言反馈
	$('.feedback').click(function(){ 	   
		$("#index-shadow").css({"height":bodyH+"px"}).show();
		$(".index-feedback-float").show();
	});
	$('.index-feedback-float .float-close').click(function(){ 	   
		$("#index-shadow").css({"height":bodyH+"px"}).hide();
		$(".index-feedback-float").hide();
	});
	
	$(".ljzc").click(function(){ 
		$(".index-login-float").hide();
		$(".index-reg-float").show();
	});			  
	
	$(".wjmm").click(function(){
		$(".changelogin").hide();
		$(".findpassword").show();
	});
	
	//按钮颜色
	$(".btn-box,.feedbackBtn,.writeBtn,.dfbt").hover(function(e){
		$(this).css({"background":"#80B505"});							 
	},function(e){
		$(this).css({"background":"#93D300"});		
	});
	
	//顶部头像点击
	$(document).click(function(e){
	  if($(e.target).is(".user-center-img img")){
	   return false;
	  }
	  $(".user-img-btshover").hide();	
	  $(".gbi5").hide();	
	});
	$(".user-center-img img").click(function(){
									   
			$(".user-img-btshover").show(); 
			$(".gbi5").show();
	});
	
	$(".love li:last").css("border-bottom","none");
	$(".youlove li,.leftimglist li").hover(function(){
		$(this).find("span").css({"filter":"alpha(opacity=70)","-moz-opacity":"0.7","opacity":"0.7"});
	},function(){
		$(this).find("span").css({"filter":"alpha(opacity=60)","-moz-opacity":"0.6","opacity":"0.6"});
	});	
	$(".plbt").hover(function(){
		$(this).css({"background-image":"url("+cdnurl+"/home/images/icon.gif)","background-position":"2px -259px"});

	},function(){
		$(this).css({"background-image":"url("+cdnurl+"/home/images/icon.gif)","background-position":"2px -216px"});
	});
	
	$(".imglists li").hover(function(){
		$(this).find("span").css({"filter":"alpha(opacity=70)","-moz-opacity":"0.7","opacity":"0.7"});
	},function(){
		$(this).find("span").css({"filter":"alpha(opacity=60)","-moz-opacity":"0.6","opacity":"0.6"});
	});
});
////end

